
figure_5 <-
  estimation_data %>%
  filter(!is.na(validated_election_outcome)) %>%
  mutate(elected = as.factor(validated_election_outcome)) %>%
  select(ID, A, elected, starts_with("avg_score")) %>%
  pivot_longer(cols = avg_score_internal) %>%
  filter(name %in% c("avg_score_internal")) %>%
  group_by(A, elected, name) %>%
  summarise(val = mean(value, na.rm = T),
            sd = sd(value, na.rm = T),
            n_val = n()) %>%
  mutate(se_val = sd/sqrt(n_val)) %>%
  ggplot(aes(x=A, y = val, group = elected, color = elected)) +
  geom_errorbar(aes(ymin=val - se_val*1.96, ymax = val + se_val*1.96), width = 0, position = position_dodge(width = 0.05)) +
  geom_pointpath(size = 1, mult = .5, position = position_dodge(width = 0.05)) +
  theme_bw() +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        #legend.position = "none",
        axis.title.x = element_blank(),
        strip.background = element_blank(),
        legend.title = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.border = element_blank(),
        legend.position = "bottom") +
  scale_color_manual(values = c("black", "darkgrey")) +
  geom_vline(xintercept = 2.2, linetype = "dashed", color = "black") +
  scale_x_continuous(breaks = c(1, 2, 3),
                     labels = c("Wave 1\n(Nov. 2023)", "Wave 2\n(Jan. 2024)", "Wave 3\n(Apr. 2024)")) +
  ylab("Internal Explanations\n(Index, 1-4)")

figure_6 <-
  estimation_data %>%
  filter(!is.na(validated_election_outcome)) %>%
  mutate(elected = as.factor(validated_election_outcome)) %>%
  select(ID, A, elected, starts_with("avg_score")) %>%
  pivot_longer(cols = avg_score_external) %>%
  filter(name %in% c("avg_score_external")) %>%
  group_by(A, elected, name) %>%
  summarise(val = mean(value, na.rm = T),
            sd = sd(value, na.rm = T),
            n_val = n()) %>%
  mutate(se_val = sd/sqrt(n_val)) %>%
  ggplot(aes(x=A, y = val, group = elected, color = elected)) +
  geom_errorbar(aes(ymin=val - se_val*1.96, ymax = val + se_val*1.96), width = 0, position = position_dodge(width = 0.05)) +
  geom_pointpath(size = 1, mult = .5, position = position_dodge(width = 0.05)) +
  theme_bw() +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        #legend.position = "none",
        axis.title.x = element_blank(),
        strip.background = element_blank(),
        legend.title = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.border = element_blank(),
        legend.position = "bottom") +
  scale_color_manual(values = c("black", "darkgrey")) +
  geom_vline(xintercept = 2.2, linetype = "dashed", color = "black") +
  scale_x_continuous(breaks = c(1, 2, 3),
                     labels = c("Wave 1\n(Nov. 2023)", "Wave 2\n(Jan. 2024)", "Wave 3\n(Apr. 2024)")) +
  ylab("External Explanations\n(Index, 1-4)")

ggsave(filename = "./outputs/figures/figure_a5a.pdf", plot = figure_5, width = 5, height = 4)
ggsave(filename = "./outputs/figures/figure_a5a.eps", device = "eps", plot = figure_5, width = 5, height = 4)

ggsave(filename = "./outputs/figures/figure_a5b.pdf", plot = figure_6, width = 5, height = 4)
ggsave(filename = "./outputs/figures/figure_a5b.eps", device = "eps", plot = figure_6, width = 5, height = 4)
